home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / MPW IIGS SC / SC.001.Shell / Pascal Shell / Shell.p < prev    next >
Encoding:
Text File  |  1990-06-24  |  13.7 KB  |  547 lines  |  [TEXT/MPS ]

  1. {
  2. *    Standard Application Shell  - Pascal Version
  3. *            Developer Technical Support
  4. *
  5. *        v3.0    Luther
  6. }
  7.  
  8. {
  9. *     Copyright (c) Apple Computer, Inc. 1988-1990
  10. *                All Rights Reserved.
  11. *
  12. *     Developer Technical Support Apple II Sample Code
  13. *
  14. *    ------------------------------------------------------
  15. *
  16. *    This program and its derivatives are licensed only for
  17. *    use on Apple computers.
  18. *
  19. *    Works based on this program must contain and
  20. *    conspicuously display this notice.
  21. *
  22. *    This software is provided for your evaluation and to
  23. *    assist you in developing software for the Apple IIGS
  24. *    computer.
  25. *
  26. *    DISCLAIMER OF WARRANTY
  27. *
  28. *    THE SOFTWARE IS PROVIDED "AS IS" WITHOUT
  29. *    WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  30. *    WITH RESPECT TO ITS MERCHANTABILITY OR ITS FITNESS
  31. *    FOR ANY PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO
  32. *    THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
  33. *    YOU.  SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU (AND
  34. *    NOT APPLE OR AN APPLE AUTHORIZED REPRESENTATIVE)
  35. *    ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING,
  36. *    REPAIR OR CORRECTION.
  37. *
  38. *    Apple does not warrant that the functions
  39. *    contained in the Software will meet your requirements
  40. *    or that the operation of the Software will be
  41. *    uninterrupted or error free or that defects in the
  42. *    Software will be corrected.
  43. *
  44. *    SOME STATES DO NOT ALLOW THE EXCLUSION
  45. *    OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY
  46. *    NOT APPLY TO YOU.  THIS WARRANTY GIVES YOU SPECIFIC
  47. *    LEGAL RIGHTS AND YOU MAY ALSO HAVE OTHER RIGHTS
  48. *    WHICH VARY FROM STATE TO STATE.
  49. }
  50.  
  51.  
  52. program Shell;
  53.  
  54. uses
  55.     Types,
  56.     GSOS,
  57.     Locator,
  58.     ADB,
  59.     IntMath,
  60.     TextTool,
  61.     Memory,
  62.     SANE,
  63.     ACE,
  64.     Resources,
  65.     MiscTool,
  66.     Scheduler,
  67.     Loader,
  68.     Quickdraw,
  69.     QDAux,
  70.     Events,
  71.     Controls,
  72.     Windows,
  73.     Menus,
  74.     LineEdit,
  75.     Dialogs,
  76.     Sound,
  77.     NoteSyn,
  78.     NoteSeq,
  79.     MIDI,
  80.     StdFile,
  81.     Scrap,
  82.     Desk,
  83.     Lists,
  84.     Fonts,
  85.     Print,
  86.     TextEdit,
  87.     Video;
  88.  
  89. const
  90.     { menu item numbers for standard DA menu items }
  91.     UndoID        = 250;
  92.     CutID        = 251;
  93.     CopyID        = 252;
  94.     PasteID        = 253;
  95.     ClearID        = 254;
  96.     CloseID        = 255;
  97.  
  98.     { application menu item numbers }
  99.     AboutID            = $1101;    { 1st item of 1st menu of 1st menu bar }
  100.     QuitID            = $1202;    { 2nd item of 2nd menu of 1st menu bar }
  101.     
  102.     { application menu numbers }
  103.     AppleMenuID        = $1100;    { 1st menu of 1st menu bar }
  104.     FileMenuID        = $1200;    { 2nd menu of 1st menu bar }
  105.     EditMenuID        = $1300;    { 3rd menu of 1st menu bar }
  106.  
  107.     { resource ID numbers }
  108.     BaseResID        = $00000000;    { start of resource ID numbers }
  109.     MenuBarOneRID    = $00001000;    { resource ID of menu bar }
  110.  
  111.     MyTaskMask        = $001FFFFF;{ handle all events possible }
  112.         
  113. var
  114.     { Standard global variables here }
  115.     MyMemoryID        : integer;    { application's memory ID }
  116.     Done            : boolean;    { flag to show when to quit application }
  117.     ToolRecRef        : Ref;        { StartStopRecRef from StartUpTools }
  118.     WindowKind        : integer;    { type of top window from GetWKind call }
  119.     MenuHeight        : integer;    { stored height of menu bar }
  120.  
  121.     { The following is the record that is used by TaskMaster to return
  122.       events. It is similar to a regular event record, except that there are
  123.       additional fields at the end. The first additional field is used to
  124.       convey some TaskMaster specific data back to the application. The second
  125.       additional field is called the TaskMask and is used to tell TaskMaster
  126.       what situations to handle.  In this shell, we tell TaskMaster to handle
  127.       everything by setting all currently defined bits to 1 (MyTaskMask) in
  128.       the initApp procedure. }
  129.  
  130.     MyEvent            : wmTaskRec;
  131.  
  132.  
  133. {******************************************************************************
  134. *
  135. * errorCheck:    This procedure is declared forward. This lets you check for
  136. *                fatal errors and still shut down fairly cleanly from
  137. *                anywhere in your program.
  138. }
  139.  
  140. procedure errorCheck(where : Integer);
  141.     FORWARD;
  142.  
  143.  
  144. {******************************************************************************
  145. *
  146. * doQuit:        Set the Done flag to true. This tells the Event loop to exit.
  147. *
  148. * Inputs:        NONE
  149. * Outputs:        Done set to true
  150. * Calls:        NONE
  151. }
  152.  
  153. procedure doQuit;
  154.  
  155. begin
  156.     Done := true;
  157. end;
  158.  
  159.  
  160. {******************************************************************************
  161. *
  162. * doAbout:        Bring up an Alert Dialog box with our about message in it.
  163. *
  164. * Inputs:        NONE
  165. * Outputs:        NONE
  166. * Calls:        NONE
  167. }
  168.  
  169. procedure doAbout;
  170.  
  171. const
  172.     alertFlags    = 4;        { reference is a ResourceID }
  173.  
  174. var
  175.       buttonHit    : integer;        { button number clicked }
  176.  
  177. begin
  178.     buttonHit := AlertWindow(alertFlags,NIL,Pointer(BaseResID+1));
  179. end;
  180.  
  181.  
  182. {******************************************************************************
  183. *
  184. * doMenu:        This routine is called when TaskMaster returns a menu
  185. *                event. It takes the menu item that was hit and calls the
  186. *                proper routine, and then unhilites the menu when it is done.
  187. *
  188. * Inputs:        TaskData holds menu item selected.
  189. * Outputs:        NONE
  190. * Calls:        doAbout, doQuit
  191. }
  192.  
  193. procedure doMenu;
  194.     
  195. const
  196.     alertFlags    = 4;            { reference is a ResourceID }
  197. var
  198.       menuNum,                    { ID of menu from which selection was made }
  199.     itemNum        : integer;        { ID of selected menu item }
  200.       buttonHit    : integer;        { button number clicked }
  201.  
  202. begin
  203.     menuNum := HiWord(MyEvent.wmTaskData);    { get menu ID }
  204.     itemNum := LoWord(MyEvent.wmTaskData);    { and item ID from MyEvent}
  205.     
  206.     case itemNum of
  207.         AboutID    : doAbout;    { show About alert }
  208.         QuitID    : doQuit;    { set Done flag }
  209.         UndoID    :;
  210.         CutID    :;
  211.         CopyID    :;
  212.         PasteID    :;
  213.         ClearID    :;
  214.         CloseID :;            { close taken care of by TaskMaster }
  215.         otherwise
  216.             buttonHit := AlertWindow(alertFlags,NIL,Pointer(BaseResID+2));
  217.     end;
  218.  
  219.     {     The routine has been called. Unhilite the menu and return to the
  220.         Main Event Loop. }
  221.  
  222.     HiLiteMenu(false,menuNum);
  223. end;
  224.  
  225.  
  226. {******************************************************************************
  227. *
  228. * doSysChange:    Called by testTopWindow when the active window
  229. *                has changed to or from a system window.
  230. *
  231. * Inputs:        Bit 15 of WindowKind is 0 if top window is an application
  232. *                window, 1 if top window is a system window.
  233. * Outputs:        NONE
  234. * Calls:        NONE
  235. }
  236.  
  237. procedure doSysChange;
  238.  
  239. begin
  240.     if WindowKind < 0    { if bit 15 of WindowKind = 1 }
  241.         then
  242.             begin
  243.                 { enable the edit menu items and the close item }
  244.                 EnableMItem(UndoID);
  245.                 EnableMItem(CutID);
  246.                 EnableMItem(CopyID);
  247.                 EnableMItem(PasteID);
  248.                 EnableMItem(ClearID);
  249.                 EnableMItem(CloseID);
  250.                 
  251.                 { if your edit menu has items that are selectable when a
  252.                   NDA is not the active window, remove the next two lines. }
  253.                 SetMenuFlag(enableMenu,EditMenuID);
  254.                 HiliteMenu(false, EditMenuID);
  255.             end
  256.         else
  257.             begin
  258.                  { disable the edit menu items and the close item }
  259.                 DisableMItem(UndoID);
  260.                 DisableMItem(CutID);                
  261.                 DisableMItem(CopyID);                
  262.                 DisableMItem(PasteID);                
  263.                 DisableMItem(ClearID);                
  264.                 DisableMItem(CloseID);
  265.                 
  266.                 { if your edit menu has items that are selectable when a
  267.                   NDA is not the active window, remove the next two lines. }
  268.                 SetMenuFlag(disableMenu,EditMenuID);
  269.                 HiliteMenu(false, EditMenuID);
  270.             end;
  271. end;
  272.  
  273.  
  274. {******************************************************************************
  275. *
  276. * testTopWindow:This routine is called on every time through the event loop.
  277. *                If the type to the top window has changed from application
  278. *                window to system window or back, this routine will call
  279. *                doSysChange.
  280. *
  281. * Inputs:        NONE
  282. * Outputs:        NONE
  283. * Calls:        doSysChange
  284. }
  285.  
  286. procedure testTopWindow;
  287.  
  288. var
  289.       tempWindowPtr    : WindowPtr;    { active window's grafPort }
  290.     tempWindowKind    : integer;    { active window's kind }
  291.  
  292. begin
  293.     tempWindowPtr := FrontWindow;    { get active window's grafPort }
  294.     
  295.     if tempWindowPtr <> NIL     { if there is an active window }
  296.         then tempWindowKind := GetWKind(tempWindowPtr) { get its kind }
  297.         else tempWindowKind := 0; { force to application window kind }
  298.         
  299.     if tempWindowKind <> WindowKind 
  300.         then                     { window kind has changed }
  301.             begin                 { save the WindowKind and change the menus }
  302.                 WindowKind := tempWindowKind;
  303.                 doSysChange;
  304.             end;
  305. end;
  306.  
  307.  
  308. {******************************************************************************
  309. *
  310. * closeTools:    Shut down the tools I started.
  311. *
  312. * Inputs:        NONE
  313. * Outputs:        NONE
  314. * Calls:        NONE
  315. }
  316.  
  317. procedure closeTools;
  318.  
  319. begin
  320.     { shut down tools started by StartUpTools }
  321.     ShutDownTools(refIsHandle,ToolRecRef);
  322.     
  323.     { shut down Memory Manager and Tool Locator }
  324.     MMShutDown(MyMemoryID);
  325.     TLShutDown;
  326. end;
  327.  
  328.  
  329. {******************************************************************************
  330. *
  331. * closeApp:        Close down things. This disposes of all items and
  332. *                memory that we allocated. Usually undoes what was done
  333. *                in initApp.  We don't close our window since _WindShutDown
  334. *                does it for us.
  335. *
  336. * Inputs:        NONE
  337. * Outputs:        NONE
  338. * Calls:        NONE
  339. }
  340.  
  341. procedure closeApp;
  342.  
  343. begin
  344.     { do nothing in this shell }
  345. end;
  346.  
  347.  
  348. {******************************************************************************
  349. *
  350. * eventLoop:    The Event Loop. Handle things until user selects Quit.
  351. *
  352. * Inputs:        NONE
  353. * Outputs:        NONE
  354. * Calls:        testTopWindow, doMenu
  355. }
  356.  
  357. procedure eventLoop;
  358.  
  359. var
  360.       taskCode    : integer;        { code indicating action to be taken }
  361.  
  362. begin
  363.     repeat
  364.         testTopWindow;            { test top window to see if it is a NDA }
  365.         
  366.         taskCode := TaskMaster(EveryEvent,MyEvent);
  367.         case taskCode of        { handle the event for this taskcode }
  368.             {    With most of these events, we do nothing (in fact, most
  369.                 applications will never see some of these events). You
  370.                 should cut the labels for events your application does
  371.                 not use out of this case statement. Any of these events
  372.                 your application does use should call a procedure to handle
  373.                 the event.    }
  374.             nullEvt:;
  375.             mouseDownEvt:;
  376.             mouseUpEvt:;
  377.             keyDownEvt:;
  378.             autoKeyEvt:;
  379.             updateEvt:;
  380.             activateEvt:;
  381.             switchEvt:;
  382.             deskAccEvt:;
  383.             driverEvt:;
  384.             app1Evt:;
  385.             app2Evt:;
  386.             app3Evt:;
  387.             app4Evt:;
  388.             wInDesk:;
  389.             wInMenuBar,                { do "In system menu bar" events and }
  390.             wInSpecial:    doMenu;        { "Item ID selected was 250-255" events }
  391.             wClickCalled:;
  392.             wInContent:;
  393.             wInDrag:;
  394.             wInGrow:;
  395.             wInGoAway:;
  396.             wInZoom:;
  397.             wInInfo:;
  398.             wInDeskItem:;
  399.             wInFrame:;
  400.             wInactMenu:;
  401.             wClosedNDA:;
  402.             wCalledSysEdit:;
  403.             wTrackZoom:;
  404.             wHitFrame:;
  405.             wInControl:;
  406.             wInControlMenu:;
  407.         end;
  408.     until Done;                        { Loop until "Quit" is selected }
  409. end;
  410.  
  411.  
  412. {******************************************************************************
  413. *
  414. * initApp:        Perform any application specific initialization. For this app,
  415. *                we initialize the Done to false, set WindowKind to an
  416. *                application window kind, initialize the TaskMask in the event
  417. *                record, and initialize all of the menus.
  418. *                .
  419. *                You might use this procedure to create windows,
  420. *                initialize variables and allocate memory needed for
  421. *                the entire program.
  422. *
  423. * Inputs:        NONE
  424. * Outputs:        NONE
  425. * Calls:        NONE
  426. }
  427.  
  428. procedure initApp;
  429.  
  430. begin
  431.     Done := false;                { we aren't done yet }
  432.     
  433.     WindowKind := 0;            { window kind  = application }
  434.     
  435.     { tell TaskMaster what events to handle }
  436.     MyEvent.wmTaskMask := MyTaskMask;
  437.     
  438.     { create default system menu bar from a resource
  439.       and make it the current menu bar }
  440.       
  441.     SetSysBar(NewMenuBar2(refIsResource,Ref(MenuBarOneRID),NIL));
  442.     SetMenuBar(NIL);
  443.     
  444.     RefreshDeskTop(NIL);        { redraw the desktop }
  445.     
  446.     InitCursor;                    { normal arrow cursor }
  447.     
  448.     FixAppleMenu(AppleMenuID);    { add NDAs to Apple menu }
  449.     MenuHeight := FixMenuBar;    { set menu bar height }
  450.     DrawMenuBar;                { draw the menu bar }
  451. end;
  452.  
  453.  
  454. {******************************************************************************
  455. *
  456. * errorCheck:    This routine is called by initTools to check for startup
  457. *                errors. An error message is shown and everything is
  458. *                shut down if any errors are detected.
  459. *
  460. * Inputs:        where = the reference number that tells you where in the
  461. *                initTools procedure the error happened.
  462. * Outputs:        NONE (program exits)
  463. * Calls:        closeTools
  464. }
  465.  
  466. procedure errorCheck(where : Integer);
  467.  
  468. var
  469.     theError    : integer;        { the tool error number }
  470.       errStr        : str255;        { string to display error message }
  471.     tempChar    : integer;        { temp to eat character returned }
  472.  
  473. begin
  474.     if _toolErr <> 0 { _toolErr is an external var }
  475.         then
  476.             begin
  477.                 theError := _toolErr;    { store the error number }
  478.                 
  479.                 { initialize errStr }
  480.                 errStr := 
  481.             'Fatal Error $xxxx has occurred at xxxx. Press any key to exit:';
  482.     
  483.                 { Stick error # into a string }
  484.                 Int2Hex(theError,Pointer(Ord4(@errStr)+14),4);
  485.  
  486.                 { Stick loc # into a string }
  487.                 Int2Hex(where,Pointer(Ord4(@errStr)+35),4);
  488.     
  489.                 GrafOff;                        { turn off super Hires }
  490.                 WriteLine(errStr);                { write errStr to text screen }
  491.                 SysBeep;                        { ring the bell }
  492.                 tempChar := ReadChar(noEcho);    { & wait for keypress }
  493.     
  494.                 closeTools;        { ShutDown my Tools }
  495.                 Halt;            { quit with APW status = 1 }
  496.                                 { Halt may be a compiler specific procedure }
  497.             end;
  498. end;
  499.  
  500.  
  501. {******************************************************************************
  502. *
  503. * initTools:    Load and startup the tools needed. errorCheck is called
  504. *                after each startup to check for errors.
  505. *
  506. * Inputs:        NONE
  507. * Outputs:        NONE
  508. * Calls:        errorCheck
  509. }
  510.  
  511. procedure initTools;
  512.  
  513. begin
  514.     TLStartUp;                    { start up Tool Locator }
  515.     errorCheck(1);                { Make sure all is OK }
  516.  
  517.     MyMemoryID := MMStartUp;    { start up Memory Manager & get Memory ID }
  518.     errorCheck(2);                { Make sure all is OK }
  519.     
  520.     { start up the rest of the tools }
  521.     ToolRecRef := StartUpTools(MyMemoryID,refIsResource,Ref(BaseResID+1));
  522.     errorCheck(3);                { Make sure all is OK }
  523. end;
  524.  
  525.  
  526. {******************************************************************************
  527. *
  528. * main:            This is the main routine. It calls procedures to startup
  529. *                the tools, initialize application specific data, run the
  530. *                main eventLoop, close the application, and shutdown the tools.
  531. *                
  532. * Inputs:        NONE
  533. * Outputs:        NONE
  534. * Calls:        initTools, initApp, eventLoop, closeApp, closeTools
  535. }
  536.  
  537. begin
  538.     initTools;                { Initialize tools. }
  539.     initApp;                { Initialize application specific stuff. }
  540.  
  541.     eventLoop;                { Do application stuff until user wants to
  542.                               do something else! }
  543.  
  544.     closeApp;                { ShutDown application specific things. }
  545.     closeTools;                { ShutDown the tools. }
  546. end.
  547.